package com.aebiz.usercenter.auth.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.aebiz.usercenter.auth.dao.IAMenuDao;
import com.aebiz.usercenter.auth.dao.IARoleMenuDao;
import com.aebiz.usercenter.auth.pojo.AMenu;
import com.aebiz.usercenter.auth.pojo.ARoleMenu;
import com.aebiz.usercenter.auth.service.IAuthService;
import com.aebiz.usercenter.common.util.ConstatFinalUtil;
import com.aebiz.usercenter.common.util.PageInfoUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 权限Service实现类
 * @author WangshSxt
 *
 */
@Service("authService")
public class AuthServiceImpl implements IAuthService
{
	@Resource
	private IAMenuDao menuDao ;
	@Resource
	private IARoleMenuDao roleMenuDao;
	/*---- 菜单操作开始 ----*/
	@Override
	public JSONObject saveOneMenuService(AMenu menu) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public JSONObject updateOneMenuService(AMenu menu) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public JSONObject deleteOneMenuService(Map<String, Object> condMap) {
		JSONObject resultJSON = new JSONObject() ; 
		int res =  this.menuDao.delete(condMap);
		
		if(res > 0 )
		{
			/* 成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			JSONObject dataJSON = new JSONObject() ; 
			/* 影响的条数 */
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
		}else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON ; 
	}
	@Override
	public AMenu findOneMenuService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Map<String, Object> findCondListMenuService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		Map<String, Object> resultMap=new HashMap<>();
		/* 处理关键字的模糊匹配 */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") +  "%");
		}
		if(pageInfoUtil != null)
		{
			/* 分页查询 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AMenu> menuList = this.menuDao.findList(condMap);
			/* 分页的两个要素 */
			/* 分页的总记录数 */
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			resultMap.put("list", menuList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			resultMap.put("pageInfoUtil", pageInfoUtil);
			return resultMap ; 
		}
		List<AMenu> menuList = this.menuDao.findList(condMap);
		resultMap.put("list", menuList);
		return resultMap;
	}
	@Override
	public String proccedMenuTreeStr(String operType, String roleId) {
		ConstatFinalUtil.SYS_LOGGER.info("===========菜单树形菜单展示================");
		/*储存结果的值*/
		StringBuffer sb=new StringBuffer();
		if("roleMenu".equalsIgnoreCase(operType)){
			sb.append("<ul class='tree treeFolder treeCheck expand' oncheck='kkk'>");
		}else{
			sb.append("<ul class='tree treeFolder collapse'>");
		}
		/*查询一级节点*/
		Map<String,Object> condMap=new HashMap<>();
		condMap.put("parentId", "0");
		Map<String, Object> resultMap=this.findCondListMenuService(null, condMap);
		List<AMenu> menusList=(List<AMenu>) resultMap.get("list");
		for (Iterator iterator = menusList.iterator(); iterator.hasNext();) {
			AMenu menu = (AMenu) iterator.next();
			this.listMenuTreeUtil(sb,menu, 0,operType,roleId);
		}
		sb.append("</ul>");
		return sb.toString();
	}
	/**
	 * 列出所有的菜单树
	 * @param sb
	 * @param menu
	 * @param i
	 * @param operType
	 * @param roleId
	 */
	private void listMenuTreeUtil(StringBuffer sb, AMenu menu, int level, String operType, String roleId) 
	{
		/*前置字符串*/
		String preStr="";
		for (int i = 0; i <level; i++) {
			preStr +="|-->";
		}
		System.out.println(preStr + menu.getName());
		if("roleMenu".equalsIgnoreCase(operType))
		{
			/* 有复选框
			 * <li><a tname="name" tvalue="value1" checked="true">我的主页</a></li>
			 * 有权限的时候才能加上选中的效果
			 * 判断是否有权限应该去查询中间表:roleMenu
			 * 根据roleId和MenuId一块查询中间表,如果能够查询到说明有权限,如果查询不到,说明木有权限
			 *  */
			/* 根据RoleId和MenuId,查询中间表 */		
			Map<String, Object> condMap=new HashMap<>();
			condMap.put("menuId", menu.getId());
			condMap.put("roleId", roleId);
			/* 分页的目的是为了获取总记录数 */
			PageInfoUtil pageInfoUtil = new PageInfoUtil() ; 
			this.findCondListRoleMenuService(pageInfoUtil, condMap);
			if(pageInfoUtil.getTotalRecord() > 0)
			{
				sb.append("<li><a tname='menuId' tvalue='"+ menu.getId() +"' checked='true'>"+ menu.getName() +"</a>");
			}else
			{
				sb.append("<li><a tname='menuId' tvalue='"+ menu.getId() +"'>"+ menu.getName() +"</a>");
			}
		}else{
			/*没有复选框*/
			sb.append("<li><a href='tabsPage.html' target='navTab' rel='menuUpdate'>"+ menu.getName() +"</a>");
		}
		/* 自己调用自己呢? */
		if(menu.getChildrenMenuList().size() > 0 )
		{
			sb.append("<ul>");
			for (Iterator iterator = menu.getChildrenMenuList().iterator(); iterator.hasNext();)
			{
				AMenu childMenu = (AMenu) iterator.next();
				this.listMenuTreeUtil(sb,childMenu, level + 1,operType,roleId);
			}
			sb.append("</ul>");
		}
		sb.append("</li>");
	}
	
	/*-- -- 菜单操作结束 ----*/
	
	/*---- 角色菜单操作开始 ----*/
	@Override
	public JSONObject saveOneRoleMenuService(ARoleMenu roleMenu) {
        JSONObject resultJSON = new JSONObject() ; 
		
		int res =  this.roleMenuDao.save(roleMenu);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0 )
		{
			/* 成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			JSONObject dataJSON = new JSONObject() ; 
			/* id */
			dataJSON.put("id", roleMenu.getId());
			/* 影响的条数 */
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
		}else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON ; 
	}
	@Override
	public JSONObject updateOneRoleMenuService(ARoleMenu roleMenu) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public JSONObject deleteOneRoleMenuService(Map<String, Object> condMap) {
		JSONObject resultJSON = new JSONObject() ; 
		int res =  this.roleMenuDao.delete(condMap);
		
		if(res > 0 )
		{
			/* 成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			JSONObject dataJSON = new JSONObject() ; 
			/* 影响的条数 */
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
		}else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON ; 
	}
	@Override
	public ARoleMenu findOneRoleMenuService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Map<String, Object> findCondListRoleMenuService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		Map<String, Object> resultMap=new HashMap<>();
		if(condMap.get("keyword")!=null)
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil!=null){
			Page page=PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<ARoleMenu> roleMenuList=this.roleMenuDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			condMap.put("totalRecord",pageInfoUtil.getTotalRecord()+"");
			
			resultMap.put("list", roleMenuList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			resultMap.put("pageInfoUtil", pageInfoUtil);
			return resultMap ; 
		}
		List<ARoleMenu> roleMenuList=this.roleMenuDao.findList(condMap);
		resultMap.put("list", roleMenuList);
		return resultMap;
	}
	/*---- 角色菜单操作结束 ----*/
	
}
